<?php

class Process {
	private $Name;
	private $Start;
	public	$Elapsed;
	private $End = false;
		
	function __construct($Name, $StartingTime=false) {
		$this->Name = $Name;
		$this->Start = ($StartingTime) ? $StartingTime : time() + round(microtime(true),3);
	}
	
	function InitiateEnd() {
		$this->End = time() + round(microtime(true),3);
		$this->Elapsed = $this->End - $this->Start;
		return $this->Elapsed;
	}
	
	function End() {
		return $this->InitiateEnd();
	}
	
	function __get($member) {
		switch ($member) {
			case "Start":
				return $this->Start;
				break;
			
			case "End":
				return $this->End;
				break;
			
			case "Name":
				return $this->Name;
				break;
				
			case "Time":
				if ($this->End)
					return $this->End - $this->Start;
				else
					return round((time() + microtime(true)) - $this->Start,3);
		}
	}
}

class Timer {
	/**
	 * Array of Processes
	 *
	 * @var Process (Array)
	 */
	public static $Processes;
	
	/**
	 * Starts main Process 
	 *
	 * @return Process
	 */
	public static function Start($StartingTime=false) {
		return self::NewProcess("main",$StartingTime);
	}
	
	/**
	 * Just Returns a Timer
	 *
	 */
	public static function Dummy() {
		return new Process("temp");
	}
	
	/**
	 * Creates New Process
	 *
	 * @param String $Name
	 * @return Process
	 */
	public static function NewProcess($Name,$StartingTime=false) {
		$NewPID = sizeof(self::$Processes);
		self::$Processes[$NewPID] = new Process($Name, $StartingTime);
		return self::$Processes[$NewPID];
	}
	
	/**
	 * Returns Process Object
	 *
	 * @param Integer $PID
	 * @return Process
	 */
	public static function GetProcess($PID) {
		return self::$Processes[$PID];
	}
	
	public static function GetProcessCount() {
		return sizeof(self::$Processes) - 1;
	}
}

?>
